import { Body, Controller, Get, Post, Req } from '@nestjs/common';
import { RechargeOrder } from 'src/entities/RechargeOrder';
import { createOrderNo, errorReturn, getTime, successReturn } from 'src/utils';
import { RechargeService } from './recharge.service';

@Controller('recharge')
export class RechargeController {
    constructor(
        private service:RechargeService
    ) {}

    @Get('options')
    async options() {
        const options = await this.service.options()
        return successReturn(options)
    }

    @Post()
    async recharge(@Body('id') id:number, @Req() { userId, IS_WECHANT, IS_ALIPAY, clientSource }) {
        id = Number(id)
        try {
            
            const option = await this.service.option(id)
            if(!option) return errorReturn()

            const amount = Number(option.amount)

            let time = getTime()
            const orderNo = createOrderNo(userId)
            let ordeData:{[key in keyof RechargeOrder]? : RechargeOrder[key]} = {
                orderNo : orderNo,
                amount : option.amount,
                //rewardAmount : String(0),
                payChannel : clientSource,
                userId,
                status : 0,
                createTime : time
            }

            if(option.payAmount == null) {
                ordeData.rewardAmount = '0'
            }else {
                ordeData.rewardAmount = String(amount - Number(option.payAmount))
            }
            
        } catch (error) {
            
        }

        return errorReturn()
    }
}
